package ui.forms;

import db.UserManager;
import base.User;
import base.Calculator;
import com.sun.lwuit.*;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.geom.Dimension;
import com.sun.lwuit.layouts.*;
import com.sun.lwuit.plaf.Border;
import ui.NumericField;

/**
 * Allowes the user to set or revise the target weight.
 *
 * @author Meera, Shayan
 */
public class TargetForm extends Form implements ActionListener {

    private Font ft;
    private Label lbTWt;
    private TextArea txtMsg;
    private ButtonGroup bt;
    private RadioButton r1;
    private RadioButton r2;
    private RadioButton r3;
    private NumericField txtTWt;
    private NumericField txtCWt;
    private Command cmdCalc;
    private Command cmdSave;
    private Command cmdBack;
    private Button btnCalc;
    private Button btnSave;
    private int currentBMR;
    private int targetBMR;
    private int adjstCal;
    private Font ft1;

    public TargetForm(final Form parent) {
        setLayout(new BoxLayout(BoxLayout.Y_AXIS));

        cmdBack = new Command("Back") {

            public void actionPerformed(ActionEvent evt) {
                parent.showBack();
            }
        };
        addCommand(cmdBack);
        setBackCommand(cmdBack);

        ft = Font.createSystemFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
        ft1 = Font.createSystemFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_SMALL);
        lbTWt = new Label("Target Weight");
        lbTWt.getStyle().setFont(ft);
        lbTWt.getStyle().setBgTransparency(0);
        txtMsg = new TextArea("", 4, 7);
        txtMsg.getStyle().setFont(ft1);
        txtMsg.getStyle().setBgTransparency(0);
        txtMsg.setPreferredSize(new Dimension((getPreferredW() / 8) * 7, 100));
        txtMsg.setFocusable(false);
        txtMsg.setEditable(false);
        txtMsg.getStyle().setBorder(Border.createLineBorder(1, 0x000000));
        cmdCalc = new Command("calculate");
        btnCalc = new Button(cmdCalc);
        cmdSave = new Command("save");
        btnSave = new Button(cmdSave);
        btnSave.setPreferredW(btnCalc.getPreferredW());

        Container c1 = new Container(new BoxLayout(BoxLayout.X_AXIS));
        Label lb1 = new Label("I want to:");
        lb1.getStyle().setFont(ft);
        lb1.getStyle().setBgTransparency(0);
        c1.addComponent(lb1);

        Container c2 = new Container(new GridLayout(1, 3));
        bt = new ButtonGroup();
        r1 = new RadioButton("Lose");
        r1.getStyle().setBgTransparency(0);
        r2 = new RadioButton("Gain");
        r2.getStyle().setBgTransparency(0);
        r3 = new RadioButton("Remain Same");
        r3.getStyle().setBgTransparency(0);

        r3.setSelected(true);
        bt.add(r1);
        bt.add(r2);
        bt.add(r3);
        c2.addComponent(r1);
        c2.addComponent(r2);
        c2.addComponent(r3);

        Container c3 = new Container(new FlowLayout(Component.CENTER));
        c3.addComponent(btnCalc);
        c3.addComponent(btnSave);

        addComponent(c1);
        addComponent(c2);
        txtCWt = new NumericField("Current Weight(kg)");
        txtTWt = new NumericField("Target  Weight(kg)");
        addComponent(txtCWt);
        addComponent(txtTWt);
        addComponent(c3);
        addComponent(txtMsg);

        this.setUserData();

        addCommandListener(this);
    }

    public void setUserData() {
        User user = Context.currentUser;
        if (user.getWeight() != 0) {
            txtCWt.getTextField().setText(Float.toString(user.getWeight()));
        }
        if (user.getTargetWeight() != 0) {
            txtTWt.getTextField().setText(Float.toString(user.getTargetWeight()));
            currentBMR = (int) Calculator.getBMR(user.getSex(), user.getHeight(), user.getWeight(), user.getDateOfBirth());
            targetBMR = (int) Calculator.getBMR(user.getSex(), user.getHeight(), user.getTargetWeight(), user.getDateOfBirth());
            adjstCal = (targetBMR - currentBMR) / 3;
            txtMsg.setText("Calories to maintain BMR:           " + currentBMR + "  KCals" + '\n' + '\n' +
                    " Target weight gain/loss adjustment:                        " +
                    "                                             " + adjstCal + "KCals" + '\n' + " Total daily allowance:                 " + targetBMR + " KCals");
        }

    }

    public void actionPerformed(ActionEvent evt) {

        if (evt.getCommand() == cmdCalc) {
            if (this.validate() == 0) {
                User user = Context.currentUser;
                currentBMR = (int) Calculator.getBMR(user.getSex(), user.getHeight(), user.getWeight(), user.getDateOfBirth());
                targetBMR = (int) Calculator.getBMR(user.getSex(), user.getHeight(), user.getTargetWeight(), user.getDateOfBirth());
                adjstCal = (targetBMR - currentBMR) / 3;

                txtMsg.setText("Calories to maintain BMR:            " + '\t' + currentBMR + "KCals" + '\n' + " Target weight gain/loss adjustment:  " + adjstCal + "KCals" + '\n' + " Total daily allowance:  " + targetBMR + "KCals");
            } else {

                this.showDialog(this.validate());
            }

        }
        if (evt.getCommand() == cmdSave) {
            if (this.validate() == 0) {
                try {
                    User user = Context.currentUser;
                    user.setTargetWeight(Float.parseFloat(txtTWt.getText()));
                    user.setWeight(Float.parseFloat(txtCWt.getText()));
                    UserManager.persist(user);
                    this.showDialog(this.validate());
                } catch (Exception ex) {
                    ex.printStackTrace();
                    Dialog.show("Error", ex.toString(), "OK", "");
                }
            } else {

                this.showDialog(this.validate());
            }
        }
    }

    public int validate() {
        int result = 0;
        if (txtTWt.getText().equalsIgnoreCase("") || txtCWt.getText().equalsIgnoreCase("")) {
            result = 8;
        } else {
            switch (bt.getSelectedIndex()) {

                case 0:
                    if (Float.parseFloat(txtTWt.getText()) > Float.parseFloat(txtCWt.getText())) {
                        result = 1;
                    }
                    if (Float.parseFloat(txtTWt.getText()) == Float.parseFloat(txtCWt.getText())) {
                        result = 2;
                    }
                    if (Float.parseFloat(txtTWt.getText()) < (Float.parseFloat(txtCWt.getText()) - 3)) {
                        result = 3;
                    }

                    break;
                case 1:
                    if (Float.parseFloat(txtTWt.getText()) < Float.parseFloat(txtCWt.getText())) {
                        result = 4;
                    }
                    if (Float.parseFloat(txtTWt.getText()) == Float.parseFloat(txtCWt.getText())) {
                        result = 5;
                    }
                    if (Float.parseFloat(txtTWt.getText()) > (Float.parseFloat(txtCWt.getText()) + 3)) {
                        result = 6;
                    }
                    break;
                case 2:
                    if (Float.parseFloat(txtTWt.getText()) != Float.parseFloat(txtCWt.getText())) {
                        result = 7;
                    }
                    break;
            }
        }
        return result;
    }

    public void showDialog(int validate) {
        switch (validate) {
            case 0:
                Dialog.show("Update Successful", "User weight updated", "OK", "");
                break;
            case 1:
                Dialog.show("Error", "You chose to lose weight.Target weight should be less than current weight", "OK", "");
                break;
            case 2:
                Dialog.show("Error", "You chose to lose weight.Target weight should be less than current weight", "OK", "");
                break;
            case 3:
                Dialog.show("Error", "Recommended weight gain/loss per week is 1kg", "OK", "");
                break;
            case 4:
                Dialog.show("Error", "You chose to gain weight.Target weight should be greater than current weight", "OK", "");
                break;
            case 5:
                Dialog.show("Error", "You chose to gain weight.Target weight should be greater than current weight", "OK", "");
                break;
            case 6:
                Dialog.show("Error", "Recommended weight gain/loss per week is 1kg", "OK", "");
                break;
            case 7:
                Dialog.show("Error", "You chose to remain the same weight.Target weight should be same as current weight", "OK", "");
                break;
            case 8:
                Dialog.show("Error", "Current weight or Target weight missing", "OK", "");
                break;
        }


    }
}
